package com.huaxindata.im.model.entity;

import android.text.TextUtils;

import com.huaxindata.im.main.chat.Helper.ContactHelper;
import com.huaxindata.im.model.contact.Contact;

import java.util.Arrays;


public class ChatSysMessageBody extends ChatMessageBody {

    /**
     * 系统消息类型
     */
    public static class SysMessageType {

        //OnlineNotice#&3250_867695024421900
        public final static String ACTION_ONLINE_NOTICE = "OnlineNotice";//账号上线通知
        public final static String ACTION_UPDATE_GROUP  = "updateGroup";// 更新群
        public final static String ACTION_CREATE_GROUP  = "createGroup";// 创建群
        public final static String ACTION_DESTROY_GROUP = "destroyGroup";// 解散群
        public final static String ACTION_LEAVE_GROUP   = "leaveGroup";// 离开群
        public final static String ACTION_RQST_FED      = "requestFriend";// 添加好友/被添加好友
        public final static String ACTION_RESP_FED      = "responseFriend";// 同意/拒绝添加/删除

        public final static String ACTION_UPDATE_USERNAME  = "updateUsername";// 更新用户名
        public final static String ACTION__UPDATE_USERICON = "updateUsericon";// 更新用户头像

        //当前不做处理
        public final static String ACTION_WEB_ANNO = "webAnno";// we公告信息
        public final static String ACTION_WEB_WARN = "webWarn";// web告警信息

    }

    public ChatSysMessageBody(int type) {
        super(type);
    }

    //对应系统消息类型
    public String action;
    //拼接字符串
    public String content;
    //主要用于在聊天记录上显示最近的一条记录，或者聊天界面中系统消息提示
    public String simpleInfo;

    protected String[] systemMessageInfo;

    public String getAction() {
        return action;
    }

    public void setContent(String content) {
        this.content = content;
        getSystemMessageInfo(content);
    }

    public String[] getSystemMessageInfo() {
        return systemMessageInfo;
    }

    /**
     * 获取系统消息字符串数组
     *
     * @param content
     * @return
     */
    public String[] getSystemMessageInfo(String content) {
        if (TextUtils.isEmpty(content))
            return null;
        systemMessageInfo = content.split(ChatMessage.PREX);
        action = systemMessageInfo[0];
        return systemMessageInfo;
    }

    /**
     * 主要用于在聊天记录上显示最近的一条记录，或者聊天界面中系统消息提示
     *
     * @param content
     * @return
     */
    public CharSequence getSimpleString(String content) {
        systemMessageInfo = getSystemMessageInfo(content);
        final StringBuilder sb = new StringBuilder();
        final String tagName;
        if (systemMessageInfo.length > 2) {
            tagName = systemMessageInfo[2];
        } else {
            tagName = null;
        }
        if (SysMessageType.ACTION_CREATE_GROUP.equals(action)) {
            if (systemMessageInfo.length > 3) {
                Contact contact = ContactHelper.getContactFromFandC(systemMessageInfo[3]);
                sb.append(contact != null ? contact.nickName : systemMessageInfo[3]);
            }
            sb.append("创建了群");
            sb.append(tagName);
        } else if (SysMessageType.ACTION_DESTROY_GROUP.equals(action)) {
            //                        sb.append("群" + tagName + "已解散");
        } else if (SysMessageType.ACTION_UPDATE_GROUP.equals(action)) {
            sb.append("群信息有更新");
        } else if (SysMessageType.ACTION_LEAVE_GROUP.equals(action)) {
            final Contact contact = ContactHelper.getContactFromFandC(tagName);
            sb.append(contact != null ? contact.nickName : tagName);
            sb.append("离开本群");
        } else if (SysMessageType.ACTION_RQST_FED.equals(action)) {
            sb.append(systemMessageInfo[1] + "请求添加您为好友");
        } else if (SysMessageType.ACTION_RESP_FED.equals(action)) {
            if ("1".equals(systemMessageInfo[2])) {
                sb.append("您已添加" + systemMessageInfo[1] + "为好友");
            } else {
                sb.append(systemMessageInfo[1] + "拒绝添加好友");
            }
        } else {
            sb.append(content);
        }
        return sb;
    }


    @Override
    public String toString() {
        return "ChatSysMessageBody{" +
                "action='" + action + '\'' +
                ", content='" + content + '\'' +
                ", simpleInfo='" + simpleInfo + '\'' +
                ", systemMessageInfo=" + Arrays.toString(systemMessageInfo) +
                '}';
    }
}
